package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.pojo.DtsOrder;
import com.qf.service.CouponService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/coupon")
@CrossOrigin
public class CouponController {
    @Autowired
    private CouponService couponService;

    //http://localhost:8083/admin/coupon/list?page=1&limit=20&name=rr&type=1&status=2&sort=add_time&order=desc
    @RequestMapping("/list")
    public Object findCoupon(int page,int limit,String sort,String order,Integer type,String name,Integer status){
        IPage<DtsCoupon> dtsCoupon = couponService.selectPage(page, limit, sort, order,type,name, status);

        Map<String,Object> result =new HashMap<>();

        result.put("total",dtsCoupon.getTotal());
        result.put("items",dtsCoupon.getRecords());
        return ResponseUtil.ok(result);
    }
    //优惠券详情
    //接口地址 : http://localhost:8083/admin/coupon/read?id=11

    /**
     *
     "id": 11,
     "name": "全场通用",
     "desc": "购买任意商品可享受此优惠",
     "tag": "满100立减5元",
     "total": 100,
     "discount": 5,
     "min": 100,
     "limit": 1,
     "type": 0,
     "status": 1,
     "goodsType": 0,
     "goodsValue": [],
     "timeType": 1,
     "days": 10,
     "startTime": "2019-12-09",
     "endTime": "2019-12-24",
     "addTime": "2019-12-16 23:29:50",
     "updateTime": "2021-05-10 15:21:10",
     "deleted": false
     */
    @RequestMapping("/read")
    public Object readCoupon(Integer id){
        DtsCoupon dtsCoupon = couponService.selectOn(id);

        Map<String,Object> result =new HashMap<>();
        result.put("id",dtsCoupon.getId());
        result.put("name",dtsCoupon.getName());
        result.put("desc",dtsCoupon.getDesc());
        result.put("tag",dtsCoupon.getTag());
        result.put("total",dtsCoupon.getTotal());
        result.put("discount",dtsCoupon.getDiscount());
        result.put("min",dtsCoupon.getMin());
        result.put("limit",dtsCoupon.getLimit());
        result.put("type",dtsCoupon.getType());
        result.put("status",dtsCoupon.getStatus());
        result.put("goodsType",dtsCoupon.getGoodsType());
        result.put("goodsValue",dtsCoupon.getGoodsValue());
        result.put("timeType",dtsCoupon.getTimeType());
        result.put("days",dtsCoupon.getDays());
        result.put("startTime",dtsCoupon.getStartTime());
        result.put("endTime",dtsCoupon.getEndTime());
        return ResponseUtil.ok(result);
    }
    //优惠券使用的用户
    //http://localhost:8083/admin/coupon/listuser?page=1&limit=20&couponId=11&sort=add_time&order=desc
    @RequestMapping("/listuser")
    public Object findlistuser(int page,int limit,String sort,String order,Integer couponId,Integer userId,Integer status){
        IPage<DtsCouponUser> dtsCouponuser = couponService.selectlistuser(page, limit, sort, order,couponId,userId, status);

        Map<String,Object> result =new HashMap<>();

        result.put("total",dtsCouponuser.getTotal());
        result.put("items",dtsCouponuser.getRecords());
        return ResponseUtil.ok(result);
    }
    /**
     * ##### 2.3.1.3. 优惠劵添加
     *
     * - 接口地址 : http://localhost:8083/admin/coupon/create
     */
    @PostMapping("/create")
    public Object creatCoupon(@RequestBody String  body ){
        Integer days = JacksonUtil.parseInteger(body, "days");
        String desc = JacksonUtil.parseString(body, "desc");
        Integer discount = JacksonUtil.parseInteger(body, "discount");
        Integer endTime = JacksonUtil.parseInteger(body, "endTime");
        Integer goodsType = JacksonUtil.parseInteger(body, "goodsType");
        String goodsValue = JacksonUtil.parseString(body, "goodsValue");
        Integer limit = JacksonUtil.parseInteger(body, "limit");
        String min = JacksonUtil.parseString(body, "min");
        String name = JacksonUtil.parseString(body, "name");
        String startTime = JacksonUtil.parseString(body, "startTime");
        Integer status = JacksonUtil.parseInteger(body, "status");
        String tag = JacksonUtil.parseString(body, "tag");
        String timeTpye = JacksonUtil.parseString(body, "timeTpye");
        Integer total = JacksonUtil.parseInteger(body, "total");
        String type = JacksonUtil.parseString(body, "type");
        DtsCoupon dtsCoupon=new DtsCoupon();
        dtsCoupon.setDays(days);
        dtsCoupon.setDesc(desc);
        dtsCoupon.setGoodsType(goodsType);
        dtsCoupon.setDiscount(new BigDecimal(discount));
        dtsCoupon.setTag(tag);
        dtsCoupon.setGoodsValue(goodsValue);
        dtsCoupon.setLimit(limit);
        dtsCoupon.setName(name);
        dtsCoupon.setStatus(status);
        dtsCoupon.setTotal(total);
        couponService.creatCoupon(dtsCoupon);

        Map<String,Object> result =new HashMap<>();
        result.put("id",dtsCoupon.getId());
        result.put("name",dtsCoupon.getName());
        result.put("desc",dtsCoupon.getDesc());
        result.put("tag",dtsCoupon.getTag());
        result.put("total",dtsCoupon.getTotal());
        result.put("discount",dtsCoupon.getDiscount());
        result.put("min",dtsCoupon.getMin());
        result.put("limit",dtsCoupon.getLimit());
        result.put("type",dtsCoupon.getType());
        result.put("status",dtsCoupon.getStatus());
        result.put("goodsType",dtsCoupon.getGoodsType());
        result.put("goodsValue",dtsCoupon.getGoodsValue());
        result.put("timeType",dtsCoupon.getTimeType());
        result.put("startTime",dtsCoupon.getStartTime());
        result.put("endTime",dtsCoupon.getEndTime());
        return ResponseUtil.ok(result);
    }
    @PostMapping("/update")
    public Object updateCoupon(@RequestBody String  body ){
        Integer days = JacksonUtil.parseInteger(body, "days");
        String desc = JacksonUtil.parseString(body, "desc");
        Integer discount = JacksonUtil.parseInteger(body, "discount");
        Integer endTime = JacksonUtil.parseInteger(body, "endTime");
        Integer goodsType = JacksonUtil.parseInteger(body, "goodsType");
        String goodsValue = JacksonUtil.parseString(body, "goodsValue");
        Integer limit = JacksonUtil.parseInteger(body, "limit");
        String min = JacksonUtil.parseString(body, "min");
        String name = JacksonUtil.parseString(body, "name");
        String startTime = JacksonUtil.parseString(body, "startTime");
        Integer status = JacksonUtil.parseInteger(body, "status");
        String tag = JacksonUtil.parseString(body, "tag");
        String timeTpye = JacksonUtil.parseString(body, "timeTpye");
        Integer total = JacksonUtil.parseInteger(body, "total");
        String type = JacksonUtil.parseString(body, "type");
        DtsCoupon dtsCoupon=new DtsCoupon();
        dtsCoupon.setDays(days);
        dtsCoupon.setDesc(desc);
        dtsCoupon.setGoodsType(goodsType);
        dtsCoupon.setDiscount(new BigDecimal(discount));
        dtsCoupon.setTag(tag);
        dtsCoupon.setGoodsValue(goodsValue);
        dtsCoupon.setLimit(limit);
        dtsCoupon.setName(name);
        dtsCoupon.setStatus(status);
        dtsCoupon.setTotal(total);
        couponService.updateCoupon(dtsCoupon);

        Map<String,Object> result =new HashMap<>();
        result.put("name",dtsCoupon.getName());
        result.put("desc",dtsCoupon.getDesc());
        result.put("tag",dtsCoupon.getTag());
        result.put("total",dtsCoupon.getTotal());
        result.put("discount",dtsCoupon.getDiscount());
        result.put("min",dtsCoupon.getMin());
        result.put("limit",dtsCoupon.getLimit());
        result.put("type",dtsCoupon.getType());
        result.put("status",dtsCoupon.getStatus());
        result.put("goodsType",dtsCoupon.getGoodsType());
        result.put("goodsValue",dtsCoupon.getGoodsValue());
        result.put("timeType",dtsCoupon.getTimeType());
        result.put("days",dtsCoupon.getDays());
        result.put("startTime",dtsCoupon.getStartTime());
        result.put("endTime",dtsCoupon.getEndTime());
        return ResponseUtil.ok(result);
    }
    @PostMapping("/delete")
    public Object deleteCoupon(@RequestBody String  body ){
        Integer days = JacksonUtil.parseInteger(body, "days");
        String desc = JacksonUtil.parseString(body, "desc");
        Integer discount = JacksonUtil.parseInteger(body, "discount");
        Integer endTime = JacksonUtil.parseInteger(body, "endTime");
        Integer goodsType = JacksonUtil.parseInteger(body, "goodsType");
        String goodsValue = JacksonUtil.parseString(body, "goodsValue");
        Integer limit = JacksonUtil.parseInteger(body, "limit");
        String min = JacksonUtil.parseString(body, "min");
        String name = JacksonUtil.parseString(body, "name");
        String startTime = JacksonUtil.parseString(body, "startTime");
        Integer status = JacksonUtil.parseInteger(body, "status");
        String tag = JacksonUtil.parseString(body, "tag");
        String timeTpye = JacksonUtil.parseString(body, "timeTpye");
        Integer total = JacksonUtil.parseInteger(body, "total");
        String type = JacksonUtil.parseString(body, "type");
        DtsCoupon dtsCoupon=new DtsCoupon();
        dtsCoupon.setDays(days);
        dtsCoupon.setDesc(desc);
        dtsCoupon.setGoodsType(goodsType);
        dtsCoupon.setDiscount(new BigDecimal(discount));
        dtsCoupon.setTag(tag);
        dtsCoupon.setGoodsValue(goodsValue);
        dtsCoupon.setLimit(limit);
        dtsCoupon.setName(name);
        dtsCoupon.setStatus(status);
        dtsCoupon.setTotal(total);
        couponService.deleteCoupon(dtsCoupon);

        return ResponseUtil.ok();
    }

}
